11. Inheritance and Pointers

11 Inheritance And Pointers

1- Copy this code to your editor and save it on your VM Desktop as Wheel.cpp:

#include <iostream>
using namespace std;

class Robot
{ 
  public:
    int Speed()
    {
     return 10;        
    }
};

class Wheel: public Robot
{};

int main() 
{
    Wheel wheel1;
    Wheel *wheel1_pt = &wheel1;

    cout << "Wheel Speed Accesed by the Object= " << wheel1.Speed() <<endl;
    cout << "Wheel Speed Accesed by the Pointer= " << wheel1_pt->Speed() <<endl;
    cout << "Address of wheel 1 object= " << wheel1_pt << endl;

    return 0;
} 

2- After copying the code, navigate to your Desktop:

$ cd Desktop

3- Now, compile the code with g++:

$ g++ Wheel.cpp -o app

4- After compiling, run the executable file:

$ ./app

5- Finally, check your terminal for the speed of the robot accessed by the object(of the inherited class) as well as the pointer. And the address of the object in memory:

Inheritance Access Specifiers

Deriving a class with a public access specifier, allows all private members of the Base class to be private in the Derived class.

SOLUTION: False

Pointers

Select the number that will be printed after compiling and executing this program?

#include <iostream>
using namespace std;

int main () 
{
   int var = 10;
   int *ptr = &var;
   *ptr+=1;
   cout << "Variable=" << var <<endl;

   return 0;
}
SOLUTION: 11